<?php
/**
 * 世界购方法
 */
namespace App\Models\Authority\Other;

use App\Models\Authority\Other\OtherLog;
use App\Models\Authority\Other\OtherSigLog;
use function GuzzleHttp\json_decode;

class sjgMethod
{

    /**
     * 注册世界购账号
     *
     * @param [type] $phone
     * @param [type] $msg
     * @return void
     */
    public static function regSJG($phone, &$msg)
    {
        $oLog = new OtherLog();
        $oLog->id = getuuID();
        $oLog->other_name = 'sjg';
        $ret = false;
        try {

            $SendArr["Action"] = "UserReg"; //请求动作名称
            $SendArr["ID"] = env('Other_SJG_id'); //接口用户ID
            $SendArr["APPID"] = env('Other_SJG_appid'); //接口用户名称
            $SendArr["Mobile"] = $phone; //用户手机号
            $SendArr["PassWord"] = md5(substr($phone, -6)); //MD5加密过的用户密码
            $SendArr["Now"] = microtime(); //毫秒时间戳

            $Key = env('Other_SJG_key'); //接口密钥

            $TempArr = $SendArr;
            ksort($TempArr);
            $TempStr = http_build_query($TempArr);
            $TempStr = $Key . $TempStr . $Key;
            $Sign = md5($TempStr);
            $SendArr["sign"] = $Sign;
            $oLog->other_url = env('Other_SJG_url') . http_build_query($SendArr);
            $Result = file_get_contents($oLog->other_url);
            $oLog->postData = json_encode($SendArr);
            $oLog->returnData = $Result;
            $oLog->other_url =env('Other_SJG_url').'Action='. $SendArr["Action"];

            $Result = json_decode($Result, true);
            if ($Result['Code'] == "2000000") {
                $ret = true;
            }
            $msg = $Result['Msg'];
        } catch (Exception $ex) {
            $oLog->errorData = $ex->getMessage();
        }

        $oLog->save();
        return $ret;
    }

    /**
     * 检测是否注册世界购账号
     *
     * @param [type] $phone
     * @param [type] $msg
     * @return void
     */
    public static function checkSJG($phone, &$msg)
    {
        $oLog = new OtherLog();
        $oLog->id = getuuID();
        $oLog->other_name = 'sjg';
        $ret = false;
        try {
            //检测用户是否存在
            $SendArr["Action"] = "UserCheck"; //请求动作名称
            $SendArr["ID"] = env('Other_SJG_id'); //接口用户ID
            $SendArr["APPID"] = env('Other_SJG_appid'); //接口用户名称
            $SendArr["Mobile"] = $phone; //用户手机号
            $SendArr["Now"] = microtime(); //毫秒时间戳

            $Key = env('Other_SJG_key'); //接口密钥

            $TempArr = $SendArr;
            ksort($TempArr);
            $TempStr = http_build_query($TempArr);
            $TempStr = $Key . $TempStr . $Key;
            $Sign = md5($TempStr);
            $SendArr["sign"] = $Sign;
            $oLog->other_url = env('Other_SJG_url') . http_build_query($SendArr);
            $Result = file_get_contents($oLog->other_url);
            $oLog->postData = json_encode($SendArr);
            $oLog->returnData = $Result;
            $oLog->other_url =env('Other_SJG_url').'Action='. $SendArr["Action"];

            $Result = json_decode($Result, true);
            if ($Result['Code'] == "3000000") {
                $ret = true;
            }
            $msg = $Result['Msg'];
        } catch (Exception $ex) {
            $oLog->errorData = $ex->getMessage();
        }

        $oLog->save();
        return $ret;
    }

    /**
     * 向世界购转入积分
     *
     * @param [type] $phone
     * @param [type] $userid
     * @param [type] $money
     * @param [type] $msg
     * @return void
     */
    public static function paytoSJG($phone, $userid, $money, &$msg)
    {
        $oLog = new OtherLog();
        $oLog->id = getuuID();
        $oLog->other_name = 'sjg';
        $ret = false;
        try {
            //检测用户是否存在
            $SendArr["Action"] = "ScorePost"; //请求动作名称
            $SendArr["ID"] = env('Other_SJG_id'); //接口用户ID
            $SendArr["APPID"] = env('Other_SJG_appid'); //接口用户名称
            $SendArr["OrderName"] = date("YmdHis") . rand(111111, 999999); //订单号，为 年年年年月月日日六位随机数字
            $SendArr["Mobile"] = $phone; //用户手机号
            $SendArr["Score"] = $money; //拨过积分
            $SendArr["NotifyUrl"] = urlencode(env('Other_SJG_NotifyUrl') . $SendArr["OrderName"]); //用户激活后的异步通知地址
            $SendArr["Now"] = microtime(); //毫秒时间戳

            $Key = env('Other_SJG_key'); //接口密钥

            $TempArr = $SendArr;
            ksort($TempArr);
            $TempStr = http_build_query($TempArr);
            $TempStr = $Key . $TempStr . $Key;
            $Sign = md5($TempStr);
            $SendArr["sign"] = $Sign;

            $oLog->other_url = env('Other_SJG_url') . http_build_query($SendArr);
            $Result = file_get_contents($oLog->other_url);
            $oLog->postData = json_encode($SendArr);
            $oLog->returnData = $Result;
            $oLog->other_url =env('Other_SJG_url').'Action='. $SendArr["Action"];
            $Result = json_decode($Result, true);
            if ($Result['Code'] == "1000000") {
                $ret = true;
                $sigLog = new OtherSigLog();
                $sigLog->id = getuuid();
                $sigLog->usersid = $userid;
                $sigLog->paymoney = $money;
                $sigLog->ordernum = $SendArr["OrderName"];
                $sigLog->paystatus = 0;
                $sigLog->save();
            }
            $msg = $Result['Msg'];
        } catch (Exception $ex) {
            $oLog->errorData = $ex->getMessage();
        }

        $oLog->save();
        return $ret;
    }
}
